package cn.cs.message.tempstorage;

import java.util.Iterator;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

import cn.cs.message.Message;
import cn.cs.message.tempstorage.disk.DiskTempStorage;

/**实现一个内存的,记录暂存在缓存*/
public class MemoryTempStorage extends AbstractTempStorage
{
	private Queue<Message> queue=new ConcurrentLinkedQueue<Message>();
	
	public void init(){}
	
	@Override
	public TempStorage newTempStorage()
	{
		TempStorage tempStorage=new DiskTempStorage();
		tempStorage.setListener(listener);
		return tempStorage;
	}
	
	@Override
	public boolean hasMsg() 
	{
		return queue.size()>0;
	}

	@Override
	public void add(Message msg) 
	{
		queue.add(msg);
		listener.onIncreaseMsg(queue.size());
	}
	
	@Override
	public long size()
	{
		return queue.size();
	}
	
	@Override
	public void clear() 
	{
		queue.clear();
	}

	@Override
	public Iterator<Message> iterator() 
	{
		return queue.iterator();
	}
	
	
}
